﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Power
{
    public partial class PowerGroup_E : PageBaseAdmin
    {
        private readonly D_T_PowerGroup_Exp _Dal = new D_T_PowerGroup_Exp();

        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserToolBar1.EditInit(
              UserConst.Exp_GetUser,
              UserConst.Exp_GetButtonList,
              (i, but) =>
              {
                  switch (i)
                  {
                      case 1:
                          but.CausesValidation = false;
                          break;
                      default:
                          break;
                  }

              },
             UserConst.Exp_ToolBarErr
           );

            if (!IsPostBack)
            {
                //获取SKey,此段代码必须有
                var id = Request.Params["ID"].Exp_IntTryParse();
                ViewState.Add("ID", id);


                if (id > 0)
                {
                    var model = _Dal.GetModel(id);
                    if (model == null || !model.Flag)
                    {
                        //请注意,只要跳转回显示页面在,则必须传入Skey
                        var sKey = this.WebUserToolBar1.EditSKey;
                        this.Exp_PageAlterHref("找不到相关资料", "powergroup.aspx?" + sKey);
                    }
                    else
                    {

                        this.TextBox2.Text = model.PowerGroupName;

                    }
                }
            }



        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            var model = new M_T_PowerGroupExp();
            model.Flag = true;
            model.id = ViewState["ID"].Exp_IntTryParse();
            model.PowerGroupName = TextBox2.Text.Exp_Trim();
            model.Explain = "";

            /*
                公共的差错校验
            */
            var iRef = 0;
            var le = new Log_Type();
            var id = 0;
            if (0 >= model.id)
            {
                iRef = _Dal.Add(model);
                le = Log_Type.add;
                id = iRef;
            }
            else
            {
                /*
                    修改的专用判断
                 */
                var Temp = _Dal.ExistsExp(model.id);
                if (!Temp)
                {
                    this.Exp_PageAlter("资料不存在,可能被其他人删除");
                    return;

                }

                iRef = _Dal.Update(model);
                le = Log_Type.update;
                id = model.id;
            }

            if (iRef > 0)
            {
                this.Exp_PageWriteLog(le, "权限组信息", id);
                var sKey = this.WebUserToolBar1.EditSKey;
                this.Exp_PageAlterHref("操作成功", "powergroup.aspx?" + sKey);

            }
            else
            {
                this.Exp_PageAlter("操作失败");
            }

        }

        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var sKey = this.WebUserToolBar1.EditSKey;
            Response.Redirect("powergroup.aspx?" + sKey);
        }
    }
}
